home *** CD-ROM | disk | FTP | other *** search
Text File | 1986-08-16 | 2.2 KB | 57 lines | [TEXT/EDIT] |
- ; (koch n) draws a fractal of degree n.
- ; A graphics window must be open when koch is called.
- ;
- ; This program was translated from a Pascal program of the same name
- ; that appeared in
- ;
- ; Matthew Zeidenberg, "Snowflakes and Dragons: Drawing fractals
- ; with Macintosh Pascal", MacWorld Volume 2, Number 8, August
- ; 1985, pages 124-131.
- ;
- ; It is interesting that the program below contains no assignment
- ; statements. The original Pascal program was full of assignment
- ; statements, but the Scheme version uses local bindings instead.
-
- (define koch
- (lambda (n)
- (fractal1-for-half-window 235 100 60 n)))
-
- (define fractal1-for-half-window
- (lambda (xorig yorig scaling n)
- (letrec ((sin60 .8660254)
- (side
- (lambda (x1 y1 x2 y2 n)
- (if (= n 1)
- (draw-line (+ x1 xorig)
- (+ y1 yorig)
- (+ x2 xorig)
- (+ y2 yorig))
- (let ((xdiff (* 1.0 (- x2 x1)))
- (ydiff (* 1.0 (- y2 y1))))
- (let ((x3 (+ x1 (round (/ xdiff 3))))
- (y3 (+ y1 (round (/ ydiff 3))))
- (x4 (+ x1 (round (- (/ xdiff 2)
- (/ (* ydiff sin60)
- 3)))))
- (y4 (+ y1 (round (+ (/ ydiff 2)
- (/ (* xdiff sin60)
- 3)))))
- (x5 (+ x1 (round (/ (* xdiff 2) 3))))
- (y5 (+ y1 (round (/ (* ydiff 2) 3)))))
- (begin
- (side x1 y1 x3 y3 (- n 1))
- (side x3 y3 x4 y4 (- n 1))
- (side x4 y4 x5 y5 (- n 1))
- (side x5 y5 x2 y2 (- n 1)))))))))
- (let
- ((x1 0)
- (y1 (round (* scaling sin60)))
- (x2 (round scaling))
- (y2 (- (round (* scaling sin60))))
- (x3 (- (round scaling)))
- (y3 (- (round (* scaling sin60)))))
- (begin
- (side x1 y1 x2 y2 n)
- (side x2 y2 x3 y3 n)
- (side x3 y3 x1 y1 n))))))
-